package com.example.stock.ui.login;

import android.app.Activity;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.stock.R;
import com.example.stock.base.BaseActivity;
import com.example.stock.databinding.ActivityLoginBinding;
import com.example.stock.databinding.ActivityMainBinding;
import com.example.stock.http.HttpManager;
import com.example.stock.ui.main.MainViewModel;
import com.example.stock.ui.order.GoodsActivity;
import com.example.stock.util.SharedPreferencesUtils;
import com.example.stock.util.Utils;

import java.util.ArrayList;

public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginViewModel> {

    public static void start(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected Class<LoginViewModel> initViewModel() {
        return LoginViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        final EditText usernameEditText = mBinding.username;
        final EditText passwordEditText = mBinding.password;
        final Button loginButton = mBinding.login;

        mBinding.username.setText(SharedPreferencesUtils.getIP());
        mBinding.password.setText(SharedPreferencesUtils.getPort());

        mViewModel.getLoginFormState().observe(this, new Observer<LoginFormState>() {
            @Override
            public void onChanged(@Nullable LoginFormState loginFormState) {
                if (loginFormState == null) {
                    return;
                }
                loginButton.setEnabled(loginFormState.isDataValid());
                if (loginFormState.getUsernameError() != null) {
                    usernameEditText.setError(getString(loginFormState.getUsernameError()));
                }
                if (loginFormState.getPasswordError() != null) {
                    passwordEditText.setError(getString(loginFormState.getPasswordError()));
                }
            }
        });

        mViewModel.getLoginResult().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean loginResult) {
                if (loginResult != null && loginResult) {
                    updateUiWithUser();
                    String http = usernameEditText.getText().toString().trim();
                    String httpPort = passwordEditText.getText().toString().trim();
                    if (!TextUtils.isEmpty(http) && !TextUtils.isEmpty(httpPort)) {
                        HttpManager.getInstance().reset(http, httpPort);
                        setResult(Activity.RESULT_OK);
                        finish();
                    } else {
                        Utils.getInstance().showToast("请输入IP地址或端口号");
                    }
                }
            }
        });

        TextWatcher afterTextChangedListener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // ignore
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // ignore
            }

            @Override
            public void afterTextChanged(Editable s) {
                mViewModel.loginDataChanged(usernameEditText.getText().toString(),
                        passwordEditText.getText().toString());
            }
        };
        usernameEditText.addTextChangedListener(afterTextChangedListener);
        passwordEditText.addTextChangedListener(afterTextChangedListener);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String http = usernameEditText.getText().toString().trim();
                String httpPort = passwordEditText.getText().toString().trim();
                if (!TextUtils.isEmpty(http) && !TextUtils.isEmpty(httpPort)) {
                    mViewModel.loginCheck("http://" + http + ":" + httpPort + "/");
                } else {
                    Utils.getInstance().showToast("请输入IP地址或端口号");
                }
            }
        });
    }

    private void updateUiWithUser() {
        String welcome = getString(R.string.settings_success);
        Toast.makeText(getApplicationContext(), welcome, Toast.LENGTH_LONG).show();
    }

}